/**
 * COPYRIGHT. Harry Wu 2011. ALL RIGHTS RESERVED.
 * Project: just-http
 * Author: Harry Wu <harrywu304@gmail.com>
 * Created On: Jun 22, 2011 2:36:51 PM
 *
*/ 
package org.justframework.ext.http;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.net.URI;
import java.net.URISyntaxException;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.justframework.api.ClientConnector;
import org.justframework.api.Request;
import org.justframework.api.Response;
import org.justframework.api.rest.RestMethod;
import org.justframework.demo.Topic;

/**
 * @author whx
 *
 */
public class HttpCommonClientTest {
	
	protected static ClientConnector client;

	/**
	 * @throws java.lang.Exception
	 */
	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
		client = new HttpCommonClient();
	}

	/**
	 * @throws java.lang.Exception
	 */
	@AfterClass
	public static void tearDownAfterClass() throws Exception {
	}

	/**
	 * @throws java.lang.Exception
	 */
	@Before
	public void setUp() throws Exception {
	}

	/**
	 * @throws java.lang.Exception
	 */
	@After
	public void tearDown() throws Exception {
	}

	@Test
	public void testRpcPost() throws URISyntaxException {
		Request request = new Request();
		request.setRequestMethod(RestMethod.POST);
		request.setUri(new URI("http://localhost:8080/lab/topic"));
		request.setParam("subject", "test001?");
		request.setParam("content", "content001");
		Response response = client.rpc(request);
		System.out.println("testRpcPost response:"+response);
		assertNotNull(response.getContent());
	}
	
	@Test
	public void testRpcDelete() throws URISyntaxException {
		Request request = new Request();
		request.setUri(new URI("http://localhost:8080/lab/topic/id0"));
		request.setRequestMethod(RestMethod.DELETE);
		Response response = client.rpc(request);
		String content = (String)response.getContent();
		assertEquals("true",content);
	}
	
	@Test
	public void testRpcGet() throws URISyntaxException {
		Request request = new Request();
		request.setUri(new URI("http://localhost:8080/lab/topic/id1"));
		request.setRequestMethod(RestMethod.GET);
		Response response = client.rpc(request);
		System.out.println("testRpcGet response:"+response);
		Topic topic = response.getContent(Topic.class);
		assertEquals("id1",topic.getTopicId());
		assertEquals("subject1",topic.getSubject());
		assertEquals("content1",topic.getContent());
		String content = (String)response.getContent();
		assertTrue(content.startsWith("{") & content.endsWith("}"));
	}
	
	@Test
	public void testRpcPut() throws URISyntaxException {
		Request request = new Request();
		request.setParam("content", "content002");
		request.setUri(new URI("http://localhost:8080/lab/topic/id2"));
		request.setRequestMethod(RestMethod.PUT);
		Response response = client.rpc(request);
		String content = (String)response.getContent();
		assertEquals("true",content);
	}
}
